<!--
 * @Author: 卢景滔
 * @Date: 2021-12-20 17:21:11
 * @LastEditTime: 2021-12-22 09:27:05
 * @LastEditors: 卢景滔
 * @Description: vue-worker使用
-->
<template>
  <div><h1>vue-worker使用</h1></div>
</template>

<script>
export default {
  created() {
    // 创建子线程
    this.worker = this.$worker.create([
      {
        message: 'to child',
        func: (data) => {
          console.log('VueWorker子线程收到主线程信息:', data)
          return 'i am fine'
        },
      },
    ])

    // 主线程发生消息
    this.worker
      // postMessage第二个参数必须是数组
      .postMessage('to child', ['how are you'])
      .then((res) => {
        console.log('VueWorker主线程收到子线程信息:', res)
      })
      .catch((e) => {
        console.error(e)
      })
  },
  destroyed() {
    // 记得销毁线程
    this.worker = null
  },
}
</script>
